package com.test.mytest.saolei.my;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;

public class SaoleiMainApp {

	protected Shell shlSaolei;
	Map<Integer, Label> map=new HashMap<>();
	int wnum=22;
	int hnum=22;
	Button[][] btns;
	int lei=60;
	int nowLei;
	//有雷的位置
	private ArrayList<Button> allLeiList;
	//标记的雷
	private ArrayList<Button> biaojiLei=new ArrayList<>();
	
	private Composite composite;
	private Label lbl_leiTotal;
	

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SaoleiMainApp window = new SaoleiMainApp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlSaolei.open();
		shlSaolei.layout();
		while (!shlSaolei.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlSaolei = new Shell(SWT.CLOSE|SWT.MIN);
		setShellSize();
		shlSaolei.setText("saolei");
		GridLayout gl_shlSaolei = new GridLayout(1, false);
		gl_shlSaolei.verticalSpacing = 0;
		gl_shlSaolei.marginWidth = 0;
		gl_shlSaolei.marginHeight = 0;
		gl_shlSaolei.horizontalSpacing = 0;
		shlSaolei.setLayout(gl_shlSaolei);
		
		Menu menu = new Menu(shlSaolei, SWT.BAR);
		shlSaolei.setMenuBar(menu);
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("文件");
		
		Menu menu_1 = new Menu(menuItem);
		menuItem.setMenu(menu_1);
		
		MenuItem menuItem_1 = new MenuItem(menu_1, SWT.NONE);
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int[] open = new SelectDialog(shlSaolei, shlSaolei.getStyle()).open();
				hnum=open[1];
				wnum=open[0];
				lei=open[2];
				setShellSize();
				disponseAll();
				initBtn();
				shlSaolei.layout();
				composite.layout();
			}
		});
		menuItem_1.setText("选项");
		
		MenuItem menuItem_2 = new MenuItem(menu_1, SWT.NONE);
		menuItem_2.setText("统计");
		
		composite = new Composite(shlSaolei, SWT.NONE);
		
		Composite composite_1 = new Composite(shlSaolei, SWT.NONE);
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
		GridData gd_composite_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_composite_1.heightHint = 25;
		gd_composite_1.widthHint = 312;
		composite_1.setLayoutData(gd_composite_1);
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new RowData(24, SWT.DEFAULT));
		label.setText("时间");
		
		Label lbl_time = new Label(composite_1, SWT.NONE);
		lbl_time.setLayoutData(new RowData(29, SWT.DEFAULT));
		lbl_time.setText("999");
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setText("总数");
		
		lbl_leiTotal = new Label(composite_1, SWT.NONE);
		lbl_leiTotal.setLayoutData(new RowData(33, SWT.DEFAULT));
		nowLei=lei;
		lbl_leiTotal.setText(""+nowLei);
		
		initBtn();
		
		OS.SetWindowPos(shlSaolei.handle, OS.HWND_TOPMOST, 0 , 0 , 800 , 680 , SWT.NULL);
	}

	protected void disponseAll() {
		for (Button[] buttons : btns) {
			for (Button button : buttons) {
				button.dispose();
			}
		}
	}

	/**
	 * 
	 */
	public void setShellSize() {
		int width = wnum*30+10;
		int height = hnum*30+hnum+1+58;
		shlSaolei.setSize(width, height);
	}

	/**
	 * @param composite
	 */
	public void initBtn() {
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_composite = new GridLayout(wnum, false);
		gl_composite.horizontalSpacing = 0;
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		btns=new Button[hnum][wnum];
		for (int i = 0; i < hnum; i++) {
			for (int j = 0; j < wnum; j++) {
				Button btnNewButton = new Button(composite, SWT.NONE);
				btns[i][j]=btnNewButton;
				btnNewButton.setData("h", i);
				btnNewButton.setData("w", j);
				btnNewButton.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
						System.out.println(btnNewButton.getText()!=null+"======="+btnNewButton.getText().equals("X"));
						if (btnNewButton.getText()!=null&&btnNewButton.getText().equals("X")) {
							return;
						}
						Object data = btnNewButton.getData("lei");
						if (data!=null) {
							baoza();
						}else{
							wulei(btnNewButton);
						}
					}
				});
				btnNewButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseDown(MouseEvent e) {
						if (e.button==3) {
							System.out.println(e.button + "====" + btnNewButton.getText() + "==");
							if ((btnNewButton.getText() == null || btnNewButton.getText().equals("")) && btnNewButton.getEnabled()) {
								btnNewButton.setText("X");
								biaojiLei.add(btnNewButton);
								nowLei--;
								lbl_leiTotal.setText(""+nowLei);
							} else {
								btnNewButton.setText("");
								biaojiLei.remove(btnNewButton);
								nowLei++;
								lbl_leiTotal.setText(""+nowLei);
							}
							if (nowLei==0) {
								openAllWuLei();
							}
						}
					}
					
				});
				GridData gd_btnNewButton = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
				gd_btnNewButton.widthHint = 30;
				gd_btnNewButton.heightHint = 30;
				btnNewButton.setLayoutData(gd_btnNewButton);
			}
		}
		createLei();
	}
	
	void openAllWuLei(){
		for (int i = 0; i < btns.length; i++) {
			Button[] buttons = btns[i];
			for (int j = 0; j < buttons.length; j++) {
				Button button = buttons[j];
				if (button.getEnabled()&&(button.getText()==null||!button.getText().equals("X"))) {
					Object data = button.getData("lei");
					if (data!=null) {
						baoza();
						return;
					}else{
						wulei(button);
					}
				}
			}
		}
	}
	/**
	 * 无雷选中
	 * @param btn
	 */
	protected void wulei(Button btn) {
		int lei=(int) btn.getData("wulei");
		if (lei>0) {
			btn.setText(lei+"");
			btn.setEnabled(false);
		}else{
			int h = (int) btn.getData("h");
			int w= (int) btn.getData("w");
			btn.setEnabled(false);
			List<Button> aroundButton = getAroundButton(h, w);
			for (Button button : aroundButton) {//周围无雷，递归清理
				if (button.getEnabled()&&(button.getText()==null||!button.getText().equals("X"))) {
					wulei(button);
				}
			}
		}
		
		
	}
	
	/**
	 * 获取周围的方格
	 * @param h
	 * @param w
	 * @return
	 */
	public List<Button> getAroundButton(int h, int w) {
		List<Button> btn=new ArrayList<>();
		if (h - 1 >= 0) {
			Button button = btns[h - 1][w];
			btn.add(button);
		}
		if (h - 1 >= 0 && w - 1 >= 0) {
			Button button = btns[h - 1][w - 1];
			btn.add(button);
		}
		if (w - 1 >= 0) {
			Button button = btns[h][w - 1];
			btn.add(button);
		}
		if (h - 1 >= 0 && w + 1 < wnum) {
			Button button = btns[h - 1][w + 1];
			btn.add(button);
		}
		if (w + 1<wnum) {
			Button button = btns[h][w + 1];
			btn.add(button);
		}
		if (h + 1 < hnum && w + 1 < wnum) {
			Button button = btns[h + 1][w + 1];
			btn.add(button);
		}
		if (h + 1 < hnum) {
			Button button = btns[h + 1][w];
			btn.add(button);
		}
		if (h + 1 < hnum && w - 1 >= 0) {
			Button button = btns[h + 1][w - 1];
			btn.add(button);
		}
		return btn;
	}

	protected void baoza() {
		for (Button button : allLeiList) {
			button.setText("⊙");
			button.setEnabled(false);
		}
		 MessageBox box = new MessageBox(shlSaolei, SWT.OK
                  | SWT.ICON_INFORMATION | SWT.APPLICATION_MODAL | SWT.TOP);
		 box.setMessage("你死了");
		 box.open();
	}

	/**
	 * 布雷
	 */
	private void createLei() {
		try {
			allLeiList = new ArrayList<Button>();
			
			for (int i = 0; i < lei; i++) {
				Random r=new Random();
				int h = r.nextInt(hnum);
				int w = r.nextInt(wnum);
				btns[h][w].setData("lei", "lei");
				allLeiList.add(btns[h][w]);
			}
			int count=0;
			for (int i = 0; i < hnum; i++) {
				for (int j = 0; j < wnum; j++) {
					Button button = btns[i][j];
					if (button.getData("lei")==null) {
						List<Button> aroundButton = getAroundButton(i, j);
						for (Button btn : aroundButton) {
							if (btn.getData("lei")!=null) {
								count++;
							}
						}
						button.setData("wulei", count);
						count=0;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	protected void count(int i) {
		List<Integer> list=new ArrayList<>();
		int n1=i+1;
		if (n1>-1) {
			list.add(n1);
		}
		int n2=i-1;
		if (n2>-1) {
			list.add(n2);
		}
		int n3=i+wnum;
		if (n3>-1) {
			list.add(n3);
		}
		int n4=i+wnum+1;
		if (n4>-1) {
			list.add(n4);
		}
		int n5=i+wnum-1;
		if (n5>-1) {
			list.add(n5);
		}
		int n6=i-wnum;
		if (n6>-1) {
			list.add(n6);
		}
		int n7=i-wnum-1;
		if (n7>-1) {
			list.add(n7);
		}
		int n8=i-wnum+1;
		if (n8>-1) {
			list.add(n8);
		}
		System.out.println(list);
	}
}
